{% extends "base.html" %}

{% block css %}
    <style>
        #cen {
            text-align: center;
        }

        a:hover {
            color: red;
        }

        .btn-primary:hover {
            color: red;
        }

        tr:hover {
            background-color: #e7e7e7;
        }
    </style>
{% endblock %}

{% block nav %}
    {% for foo in model_list %}
        <li><a href="{{ foo.url }}"><i class="{{ foo.icon }}"></i>{{ foo.model_name }}</a>
        </li>
    {% endfor %}

{% endblock %}

{% block crumbs %}
    <div class="row">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li class="active">
                    <a class="this-page">定时任务</a>
                </li>
            </ol>

        </div>
    </div>
{% endblock %}

{% block body %}

    <div class="row">
        <div class="col-md-12">
            <div class="panel">
                <div class="panel-heading">
                    <a class="btn btn-primary" id="add_id_PeriodicTask"
                       href="{% url 'base:task_add' %}" title="增加另一个 测试用例">
                        <i class="fa fa-plus-circle"></i>&nbsp;添加任务
                    </a>&nbsp;&nbsp;&nbsp;
                    <a class="btn btn-primary" id="add_id_PeriodicTask"
                       href="http://www.easytest.xyz:5555" title="" target="_blank"><i class="fa fa-bullseye"></i>&nbsp;任务监控
                    </a>&nbsp;&nbsp;&nbsp;
                    <a href="{% url 'base:task_logs' %}" class="btn btn-primary"><i class="fa fa-file-text-o"></i>&nbsp;任务运行日志</a>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th id="cen">ID</th>
                            <th id="cen">任务名称</th>
                            <th id="cen">任务模块</th>
                            <th id="cen">执行时间</th>
                            <th id="cen">执行计划</th>
                            <th id="cen">更新时间</th>
                            <th id="cen">状态</th>
                            <th id="cen">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for task in object_list %}
                            <tr>

                                <td id="cen" class="task_id">{{ task.id }}</td>
                                <td><a class="related-widget-wrapper-link add-related"
                                       id="add_id_Apitest"
                                       href="{% url 'base:task_update' %}?task_id={{ task.id }}&&page={{ page }}">{{ task.name }}</a>
                                </td>
                                <td>{{ task.task }}</td>
                                {% if task.crontab %}
                                    <td>
                                        <span style='color:green'>{{ task.crontab.minute }} {{ task.crontab.hour }} {{ task.crontab.day_of_week }} {{ task.crontab.day_of_month }} {{ task.crontab.month_of_year }}
                                            (m/h/d/dM/MY)</span></td>
                                {% else %}
                                    {% if task.interval %}
                                        <td>
                                            <span style='color:green'>{{ task.interval.every }} {{ task.interval.period }}/次</span>
                                        </td>
                                    {% endif %}
                                {% endif %}
                                <td id="cen">{{ task.args }}</td>
                                <td id="cen">{{ task.date_changed }}</td>
                                <td id="cen">{{ task.enabled }}</td>
                                <td id="cen">
                                    <a href="#" onclick="run_delay(this)"
                                       class="layui-btn layui-btn-danger layui-btn-xs"
                                       data-toggle="modal" data-target="#runCaseForm"><i
                                            class="fa fa-recycle"></i>&nbsp;&nbsp;&nbsp;
                                    </a>
                                    <a href="{% url 'base:task_delete' %}?task_id={{ task.id }}&&page={{ page }}"
                                       onclick="return delete_btn()"
                                       class="layui-btn layui-btn-danger layui-btn-xs"><img
                                            src="/static/admin/img/icon-deletelink.svg"/>&nbsp;&nbsp;&nbsp;</a>
                                </td>

                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                    {% if error %}
                        <span style="color: red; font-size: 10px;">{{ error }}</span>
                    {% endif %}

                </div>
                {% if is_paginated %}
                    <ul class="pagination">
                        {% if first %}
                            <li class="page"><a href="?page=1">1</a></li>
                        {% endif %}
                        {% if left %}
                            {% if left_has_more %}
                                <li class="page"><a href="javascript:void(0)">...</a></li>
                            {% endif %}
                            {% for i in left %}
                                <li class="page"><a href="?page={{ i }}">{{ i }}</a></li>
                            {% endfor %}
                        {% endif %}
                        <li class="page"><a href="javascript:void(0)" class="active"> {{ page_obj.number }}</a></li>
                        {% if right %}
                            {% for i in right %}
                                <li class="page"><a href="?page={{ i }}">{{ i }}</a></li>
                            {% endfor %}
                            {% if right_has_more %}
                                <li class="page"><a href="javascript:void(0)">...</a></li>
                            {% endif %}
                        {% endif %}
                        {% if last %}
                            <li class="page"><a
                                    href="?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a>
                            </li>
                        {% endif %}

                    </ul>
                {% endif %}
            </div>

        </div>
    </div>


{% endblock %}

{% block javascript %}
    <script>

        //删除
        function delete_btn() {
            if (window.confirm("你确定要删除吗？")) {
                return true;//确定返回真
            }
            else {
                return false;//取消返回假
            }
        }

        function run_delay(obj) {
            var task_id = obj.parentNode.parentNode.children[0].innerText;
            $.ajax({
                type: "post",
                url: "/base/task_run/",
                data: {"id": task_id},
                success: function (data) {
                    if (confirm(data)) {
                        location = "/base/report_page/"
                    } else {

                    }

                },
                error: function () {
                    alert("执行失败");
                    $('.spinner').hide();
                }
            });

        }


    </script>
{% endblock %}